package com.meixiang.activity.homes.insurance;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.meixiang.R;
import com.meixiang.adapter.InsuranceBuyAdapter;
import com.meixiang.main.BaseActivity;
import com.meixiang.recyclerview.RecycleViewDivider;
import com.meixiang.view.TitleView;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author yinsujun  Created at 2016/5/3 17:02
 * 美保险
 */
public class InsuranceHistoryActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener,View.OnClickListener {
    @Bind(R.id.title)
    TitleView titleView;
    @Bind(R.id.swipe_target)
    RecyclerView swipe_target;
    List<String> data;
    @Bind(R.id.mRefresh)
    SwipeToLoadLayout mRefresh;
    private InsuranceBuyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_insurance_history);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        titleView.setTitle(R.string.insurance_history);
        titleView.setLeftImageButton(R.mipmap.back);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        swipe_target.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        swipe_target.setItemAnimator(new DefaultItemAnimator());
        swipe_target.addItemDecoration(new RecycleViewDivider(context, RecycleViewDivider.VERTICAL_LIST));
        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
        //自动刷新
        mRefresh.post(new Runnable() {
            @Override
            public void run() {
                mRefresh.setRefreshing(true);
            }
        });

        //RecyclerView滑到底部自动加载更多
        swipe_target.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
                        mRefresh.setLoadingMore(true);
                    }
                }
            }
        });

    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    data = new ArrayList<>();
                    for (int i = 0; i < 30; i++) {
                        data.add(i + "");
                    }
                    adapter.notifyDataSetChanged();
                    mRefresh.setRefreshing(false);
                    break;
                case 1:
                    if (data.size() > 50) {
                        Toast.makeText(context, "已没有更多数据", Toast.LENGTH_LONG).show();//設置已沒有更多数据
                    } else {
                        int size = data.size();
                        for (int i = size; i < size + 15; i++) {
                            data.add(i + "");
                        }
                        adapter.notifyDataSetChanged();
                    }
                    mRefresh.setLoadingMore(false);
                    break;
            }
        }
    };

    @Override
    public void onLoadMore() {
        mHandler.sendEmptyMessageDelayed(1, 1500);
    }

    @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(0, 1500);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {
        data = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            data.add(i + "");
        }

        adapter = new InsuranceBuyAdapter(context, data);
        swipe_target.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {

    }


}
